﻿using DataScripter.Database;
using DataScripter.Scripting;
using DataScripter.Scripting.Fluent;
using DataScripter.Scripting.InputSources;
using DataScripter.Scripting.ScriptDefinitions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace DataScripter.UnitTests.EngineTests.IncludeDependencies
{
    [TestClass]
    public class WhenRequestingATableHavingOneSingleDependencyAndIncludingAllDependencies
    {
        private readonly IFluentTableScriptRequest _tableScriptRequest;
        private readonly IScriptDefinition _scriptDefinition;

        public WhenRequestingATableHavingOneSingleDependencyAndIncludingAllDependencies()
        {
            const string myTableName = "MyTableWithOneOrMoreDependencies";
            Engine engine = new Engine();

            Mock<IDatabaseStructure> structureMock = Factories.DbStructureFactory.Create(myTableName, "MyDependency");
            IInputSource inputSource = new SqlServerInputSource("", engine);
            inputSource.RegisterType(structureMock.Object);
            engine.RegisterType(inputSource);

            _scriptDefinition = new ScriptDefinition("", engine);
            engine.RegisterType(_scriptDefinition);
            _tableScriptRequest = _scriptDefinition.RequestScript(myTableName).IncludeDependencies();
        }

        [TestMethod]
        public void TableAndDependencyShouldBeAppendedToTablesCollection()
        {
            Assert.AreEqual(2, _scriptDefinition.ScriptRequests.Count);
        }

        [TestMethod]
        public void TableScriptRequestShouldExistInCollection()
        {
            Assert.IsTrue(_scriptDefinition.ScriptRequests.ContainsKey(_tableScriptRequest.Request.TableName));
        }

        [TestMethod]
        public void TableScriptRequestShouldBeTheSameInCollection()
        {
            Assert.IsTrue(_scriptDefinition.ScriptRequests[_tableScriptRequest.Request.TableName] == _tableScriptRequest.Request);
        }

        [TestMethod]
        public void DependencyShouldExistInCollection()
        {
            Assert.IsTrue(_scriptDefinition.ScriptRequests.ContainsKey(_tableScriptRequest.Request.TableStructure.ForeignKeys[0].ReferencedTableName));
        }
    }
}
